home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / AlexNeXTSTEPSource / Source / Chapter3_OOD / ShapeArea / ShapeArea_main.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  136 lines

  1. #import <appkit/appkit.h>
  2. #import "Rectangle.h"
  3. #import "Square.h"
  4.  
  5. // a program that demonstrates dynamic binding
  6.  
  7. // function prototypes
  8. void createObjects(void), run(void);
  9. void calculateAreas(void), freeAll(void);
  10. void main(void);
  11. BOOL readInput(void);
  12.  
  13. // global variable
  14. id theList;
  15.  
  16. void createObjects(void)
  17. {
  18.     // create list to hold shapes
  19.     theList = [[List alloc] init];
  20. }
  21.  
  22. void run(void)
  23. {
  24.     // stay in loop until Q is pressed
  25.     BOOL done = NO;
  26.     while (!done)
  27.         {
  28.         done = readInput();
  29.         }
  30. }
  31.  
  32. BOOL readInput(void)
  33. {
  34.     char inputString[10];
  35.     unsigned int choice;
  36.     float width, height;
  37.     id object;
  38.  
  39.     // print the menu
  40.     printf("\nShapeArea calculates the ");
  41.     printf("areas of various shapes\n");
  42.     printf("==============\n");
  43.     printf("Select a shape\n");
  44.     printf("Type Q to quit\n\n");
  45.     printf("1 rectangle\n");
  46.     printf("2 square\n");
  47.     printf("===============\n");
  48.     // get input
  49.     scanf("%s", inputString);
  50.     if (strcmp(inputString, "Q") != 0)
  51.         {
  52.         choice = atoi(inputString);
  53.         switch (choice)
  54.             {
  55.             case 1:
  56.                 // get width and height
  57.                 printf("Enter the height\n");
  58.                 scanf("%f", &height);
  59.                 printf("Enter the width\n");
  60.                 scanf("%f", &width);
  61.                 // create rectangle and init it
  62.                 object = [[Rectangle alloc]
  63.                     initHeight:height
  64.                     width:width];
  65.                 // add rectangle to list
  66.                 [theList addObject:object];
  67.                 printf("The area of this shape is %.2f\n",
  68.                     [object calcArea]);
  69.                 break;
  70.             case 2:
  71.                 // get height for square
  72.                 printf("Enter the height\n");
  73.                 // since a square's height
  74.                 // is equal to its width,
  75.                 // get only one value
  76.                 scanf("%f", &height);
  77.                 // create square and init it
  78.                 object = [[Square alloc]
  79.                     initHeight:height
  80.                     width:height];
  81.                 // add square to list
  82.                 [theList addObject:object];
  83.                 printf("The area of this shape is %.2f\n",
  84.                     [object calcArea]);
  85.                 break;
  86.             default:
  87.                 // invalid choice
  88.                 printf("No such shape!\n");
  89.                 printf("Please try again!\n\n");
  90.                 break;
  91.             }
  92.         // return NO to stay in while loop
  93.         // of run() function
  94.         return NO;
  95.         }
  96.     else
  97.         // return YES to abort while loop
  98.         return YES;
  99. }
  100.  
  101. void calculateAreas(void)
  102. {
  103.     int i;
  104.     // traverse the list and request each
  105.     // instance to perform its calcArea method
  106.     // this demonstrates dynamic binding
  107.     // since it impossible to determine
  108.     // the class of each instance until runtime
  109.     for (i=0; i < [theList count]; i++)
  110.         {
  111.         printf("\nThis shape is an instance of %s\n",
  112.             [[theList objectAt:i] name]);
  113.         printf("The area of this shape is %.2f\n",
  114.             [[theList objectAt:i] calcArea]);
  115.         }
  116. }
  117.  
  118. void freeAll(void)
  119. {
  120.     // free the objects in the list
  121.     // freeObjects sends a free message
  122.     // to each object in the list
  123.     [theList freeObjects];
  124.     // free the list itself
  125.     [theList free];
  126. }
  127.  
  128. void main(void)
  129. {
  130.     createObjects();
  131.     run();
  132.     calculateAreas();
  133.     freeAll();
  134.     exit(0);
  135. }
  136.